package com.power.sifa.approve.dao;

import com.power.sifa.approve.model.Apl_decision_record;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * 干预行政复议活动记录 持久层(hibernate)
 */

public interface Apl_decision_recordDao extends JpaRepository<Apl_decision_record, Serializable>, JpaSpecificationExecutor<Apl_decision_record>, Repository<Apl_decision_record, Serializable>, Apl_decision_recordDaoCustom<Apl_decision_record, Serializable> {

   @Modifying
   @Transactional
   @Query("delete  from Apl_decision_record s where  s.id in (:ids) ")
   void removeByIds(@Param("ids") List<Integer> ids);


   /**
    * 主办人，查询还未决定的记录，当前的applyid为空
    * @param faapplyid 父节点的applyid
    * @return
    * @throws IOException
    */
   @Query("from Apl_decision_record s where s.faapplyid = :faapplyid and s.applyid is null and s.signtype = 3200")
   List<Apl_decision_record> getNoDecisionRecords(@Param("faapplyid") String faapplyid);



   /**
    * 主办人，查询决定的记录，当前的applyid为空
    * @param faapplyid 父节点的applyid
    * @return
    * @throws IOException
    */
   @Query("from Apl_decision_record s where s.faapplyid = :faapplyid and s.applyid = :applyid and s.signtype = 3200")
   List<Apl_decision_record> getNoDecisionMainRecords(@Param("faapplyid") String faapplyid, @Param("applyid") String applyid);

   /**
    * 协办人，查询还未决定的记录，当前applyid存在
    * @param faapplyid 父节点的applyid
    * @return
    * @throws IOException
    */
   @Query("from Apl_decision_record s where s.faapplyid = :faapplyid and s.applyid = :applyid and s.signtype = 3201")
   List<Apl_decision_record> getNoDecisionSubRecords(@Param("faapplyid") String faapplyid, @Param("applyid") String applyid);


   /**
    * 更新未保存记录的applyid
    * @param applyid
    * @param faapplyid
    */
   @Modifying
   @Transactional
   @Query(value = "update Apl_decision_record s set s.applyid = :applyid where s.faapplyid = :faapplyid and s.applyid is null" , nativeQuery = true)
   void updateDecisionRecords(@Param("faapplyid") String faapplyid, @Param("applyid") String applyid);


   /**
    * 查询主办人和协办人的干涉记录
    * @param faapplyid
    * @param applyid
    * @return
    */
   @Query("from Apl_decision_record s where s.faapplyid = :faapplyid and s.applyid = :applyid")
   List<Apl_decision_record> getDecisionRecords(@Param("faapplyid") String faapplyid, @Param("applyid") String applyid);

}
